/**
 * Created by caiqyxyx on 2017/3/5.
 */
define(["durandal/app", "jquery","ReconnectingWebSocket"], function (app, $,ReconnectingWebSocket) {
    var init = function(){
        var websocket;
        var websocktUrl = cywork.global.rootPath + "websocket/cywork.htm";
        
        if ('WebSocket' in window) {
            console.log("connecting to the websocket server!");
            websocket = new ReconnectingWebSocket(websocktUrl.replace("http","ws"));
        } else{
            console.log("ERROR:websocket is unsupported by the broswer!");
        }
        websocket.onopen = function(event){
            console.log("connect to the websocket success!");
        }
        websocket.onmessage = function(event){
            alert(event.data);
            console.log("onmessage");
            console.log(event);
        }
        websocket.onerror = function (event) {

        }
        websocket.onclose = function(evnet){
            console.log("lose the connection of websocket!");
        }

        return websocket;
    }

    return {
        init:function () {
            return init();
        }
    }
});